<?php
namespace app\api\controller;

use app\service\GoodsService;
use app\service\SourceService;
use app\service\UserService;
use app\common\model\Order as OrderModel;
use think\Exception;

class Index extends Base
{

    //首页信息
    public function index(){
        $backData['carousel']=SourceService::S()->selectSource(array('type'=>1),'url,title,href');//轮播图
        foreach ($backData['carousel'] as $k=>$v){
            $backData['carousel'][$k]['url']=tomedia($v['url']);
        }
        $filed='id,name,price,shop_price,picture,picture_list,ord,stock,tag,is_cart,buy_min';
        $where = [
            'onsale'=>1
        ];
        $where['is_new'] = 1;
        $backData['new_goods']=GoodsService::dealGoods(GoodsService::S()->queryGoodsInfoByWhere($where,$filed));
        $where['is_hot'] = 1;
        $backData['hot_goods']=GoodsService::dealGoods(GoodsService::S()->queryGoodsInfoByWhere($where,$filed));
        $where['is_tejia'] = 1;
        $backData['tejia_goods']=GoodsService::dealGoods(GoodsService::S()->queryGoodsInfoByWhere($where,$filed));
        $where['is_tuijian'] = 1;
        $backData['tuijian_goods']=GoodsService::dealGoods(GoodsService::S()->queryGoodsInfoByWhere($where,$filed));

        $orderList = OrderModel::getPage([
            'checked' => [
                'in',[1,2,5]
            ]
        ],1,10,'create_time desc','title,uid,create_time');
        if($orderList) {
            foreach($orderList as $k=>$v) {
                $orderList[$k]['nickname'] = UserService::S()->getUserFiled(['id'=>$v['uid']],'nickname');
            }
            $backData['new_order'] = $orderList;
        }else{
            $backData['new_order'] = [];
        }
        

        $this->ajax(200,'success',$backData);
    }

    //搜索
    public function search_goods(){
        $ord='ord';
        $page=input('post.page');
        $page_num=20;
        $goods_name=input('post.goods_name');
        if(!empty($goods_name)){
            $where['name']=array('like',"%{$goods_name}%");
        }
        $type=input('post.type');
        if(!empty($type)){
            switch ($type){
                case 'sale_real':
                    $ord='sale_real';
                    break;
                case 'price':
                    $ord='price';
                    break;
                case 'default':
                    break;
            }
        }
        $filed='id,name,price,shop_price,picture,picture_list,ord,stock,sale_real,tag';
        $goods=GoodsService::S()->queryGoodsInfoByWherePage($where,$page,$page_num,$filed,$ord);
        $goods=GoodsService::dealGoods($goods);
        $backData['code']=200;
        $backData['msg']='查询成功';
        $backData['data']=$goods;
        $backData['page_count']=ceil(GoodsService::S()->countGoods($where)/$page_num);
        echo json_encode($backData);exit();
    }


}
